package com.macro.mall.tiny.component.wx;

import com.binarywang.spring.starter.wxjava.pay.properties.WxPayProperties;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author Tyrone
 * @date 2024/11/30 16:06
 */
@Configuration
@EnableConfigurationProperties(WxPayProperties.class)
public class WxTransferConfig {
    @Autowired
    private WxPayProperties properties;

    @Bean
    public TransferBatchService transferBatchService() throws Exception {

//        byte[] keyBytes = Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource("cert/apiclient_key.pem").toURI()));
        String privateKey  =
                "-----BEGIN PRIVATE KEY-----\n" +
                        "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCw6l0DzXFA81Fy\n" +
                        "1WtlP+RYpsk699f1wbzGYbtGvQP2cWjq4yGR7JaSsOphqVhXfDNbfmldqdzAAy/f\n" +
                        "YUWHrIsxwMSOuCJAmDBLmqST49LxB3Z1eTgRvkcjq3wj8BBSAy8mjKVIXmvzha43\n" +
                        "ggrA9n2l0tP26IGsIxL46t2PnAbUANk4vMEB4/h7zqvwPBOP/hvDn9cpr76Ecg68\n" +
                        "hWtefdORkvoVV1enaFqCwIfyFtN1Ogw7kDLmZRV4FaWIkgdBWOZE1Ig27R2YcmrB\n" +
                        "/u6yiRj8nJCc6SHDgG71oDjNSG4ANXfUSQTXX/LwHb+6nX2VLHSjjJb4I24IPkl7\n" +
                        "AQWiixizAgMBAAECggEBAJJ31b46LlNpnrsFuj4/IVvL6bnqS19poFJVw2iTQF6e\n" +
                        "EhC1LqvTRe34jnOAcmUrrJFJHQ6wfNRzTwUPUTM8RJlSUOkfxSvZU9KkGzFdYtyu\n" +
                        "IwKq8yKtd9QuOz69l6VugXDW7Ff35EPfHxdRkHmlGO1amKZIZGMRPrnaKMimknid\n" +
                        "QgAztueNT7uuG6XW5G1xvXDnwgA7T2sNqYvqWcbTJrDVO0QAB98CdWcBySGcDKY6\n" +
                        "371cRe4yiYgDMo393ztxD0YX7D+7EQw7DQeSJlTFDrNHDyJSgtOTTWmRIyiUGgck\n" +
                        "XGY2Iymz00hBiCcd5CWfBpVmGFIX7xlgcEe0qS4W2PECgYEA5AhOGkzEGXirfjrI\n" +
                        "X5g/bNmvOwpMo4qAnrZVZuAB4WKp5Hwbr/iphqWpMzs7FdK+MAzghSPHavrSPlhr\n" +
                        "0xRRp4CLkI9Vf1SUH2YZW1uvfcflBSRBpyQRF7u4EOD9JFlHf46LzzVu8qd+dKnp\n" +
                        "QxHg6UbOeWycfWEAbXoYQf+q/kcCgYEAxp0aZdheUK2sN5HwtKeviJ4tyB/j+G/6\n" +
                        "V7ykChzU+xAesSuuBlWNEUmkT13gg6zPOxVGItEF5tUc2kwM9xGrukY387PTNv8a\n" +
                        "1373X4A0BNXaoJybypO0cr+BOGZ65DbhRUVNcDbovVdskhcGtMXrgluiqCVbqLt5\n" +
                        "t1Bs4Qbt7DUCgYBjtP3xof9c49aYqztSEVAqWES2nBpGqBj1TOSqxugLlQiUT3AL\n" +
                        "0b7OkUEalZuQP0XJ3NePX8WikWm8uQzsjsXlZ9+epg3MTz81O3964uwpI26QgPn0\n" +
                        "C2Et73bcaoRFkIQi0kFvqP2KPJKekGzeVsLxPXSqw0Lpz7BQLkLNGO27oQKBgB5k\n" +
                        "uj5tnvk/qoQSfEBZDN6ePilK4AsLr8avYnb9tIX7EIdSRN5JRz4nA9L1bb8Yudmz\n" +
                        "xrjbPcj2QeUSEEXa2Wm3BybvFNpsrYHetyJRLySySTOc46Zbdhi9P5wO6q1zzINa\n" +
                        "OKB8dp9K+p1ckrUlOIYJWdnbZaxrjBPGZfMFqXElAoGAL09Ek78ZxpeZL64U75fl\n" +
                        "KeUprQjFZ0BxB5zMeeOPtkm23Q4pekU1SkXfbh3vENkA5llXV1XpPDm4GpZQ6fq9\n" +
                        "YRk8UPxXMglozMIsdFlgWIPV4RUxHU/IEfGo9A3VGakJpaHFK963qg6/NNZ48Ayy\n" +
                        "5CoP/VwqVmhV0YaxJNCzKho=\n" +
                        "-----END PRIVATE KEY-----\n";
        Config config = new RSAAutoCertificateConfig.Builder()
                .merchantId(StringUtils.trimToNull(this.properties.getMchId()))
//                .privateKeyFromPath(StringUtils.trimToNull(this.properties.getPrivateKeyPath()))
//                (StringUtils.trimToNull(this.properties.getPrivateKeyPath().replace("classpath:", "")))
                .merchantSerialNumber(StringUtils.trimToNull(this.properties.getCertSerialNo()))
                .apiV3Key(StringUtils.trimToNull(this.properties.getApiv3Key()))
                .privateKey(privateKey)
                .build();
        TransferBatchService build = new TransferBatchService.Builder().config(config).build();
        return build;
    }
}
